setwd("~/Documents/replication_packages/eps_2016/")

# Note: ITANES data can be downloaded from http://www.itanes.org/

require(haven)

# No question on migration
itanes1996 <- read_sav("ITA1996_(itvers2005).sav")
itanes2001 <- read_sav("ITA2001_(itvers2007).sav")
itanes2006 <- read_sav("ITA2006_PRE_POST_NEW(vers_dic06).sav")
itanes2008 <- read_sav("ITA2008_(itvers2009).sav")
itanes2013 <- read_sav("ITA2013_(itvers2013_11_29).sav")

# Answer identifying perception on immigration

##  1996

# PARTIES in 1996
party_code_96 <-
  c(
    "PPI" = 1,
    "Patto Segni" = 2,
    "PDS" = 3,
    "Rifondazione Comunista" = 4,
    "PSI-Partito Socialista" = 5,
    "Forza Italia" = 6,
    "Alleanza Nazionale/MSI" = 7,
    "Lega Nord" = 8,
    "Lista Pannella" = 9,
    "Verdi" = 10,
    "Rete" = 11,
    "Alleanza Democratica" = 12,
    "Altro" = 13,
    "Non aveva l'eta" = 14,
    "Scheda bianca/nulla" = 15,
    "Non ha votato" = 16
  )

centreright_parties_96 <- c(6, 7, 8)

itanes1996$voted <- with(itanes1996, q133 <= 13)
itanes1996$voted_lega <- with(itanes1996, q133==8)
itanes1996$voted_lega[itanes1996$voted == FALSE] <- NA
itanes1996$voted_right <- with(itanes1996, q133 %in% centreright_parties_96)
itanes1996$voted_right[itanes1996$voted == FALSE] <- NA

# I would like to know more about your views on seven social problems. Among the following problems, which do you feel is the most serious in Italy? Before answering allow me to read the list. 
# Is there another problem you consider serious?
itanes1996$immigration_first <- with(itanes1996, q1 == 4)
itanes1996$immigration_second <- with(itanes1996, q2 == 4)
itanes1996$immigration_first_or_second <- 
  with(itanes1996, immigration_first == TRUE | immigration_second == TRUE)

with(itanes1996, prop.table(table(voted_lega, immigration_first), 1))
# immigration_first
# voted_lega      FALSE       TRUE
# FALSE 0.96610956 0.03389044
# TRUE  0.89873418 0.10126582
chisq.test(with(itanes1996, table(voted_lega, immigration_first)))
# p-value = 5.466e-05

with(itanes1996, prop.table(table(voted_lega, immigration_first_or_second), 1))
# immigration_first_or_second
# voted_lega     FALSE      TRUE
# FALSE 0.8942669 0.1057331
# TRUE  0.7452229 0.2547771
chisq.test(with(itanes1996, table(voted_lega, immigration_first_or_second)))
# p-value = 3.811e-08

rq1_1996_tbl <-
  with(subset(itanes1996, voted_right == TRUE), 
       prop.table(table(voted_lega, immigration_first_or_second), 1))
# immigration_first_or_second
# voted_lega     FALSE      TRUE
# FALSE 0.8697730 0.1302270
# TRUE  0.7452229 0.2547771

rq1_1996_pvalue <- 
  chisq.test(with(subset(itanes1996, voted_right == TRUE),
                table(voted_lega, immigration_first_or_second)))$p.value
# p-value = 0.0001003

parties_immigration_96 <-
  as.data.frame(with(subset(itanes1996, voted == TRUE), 
     prop.table(table(q133, immigration_first_or_second), 1)))

parties_immigration_96$q133 <- 
  names(party_code_96[match(parties_immigration_96$q133, party_code_96)])

parties_immigration_96 <- subset(parties_immigration_96, immigration_first_or_second == TRUE)
parties_immigration_96 <-
  merge(
    parties_immigration_96,
    as.data.frame(table(names(party_code_96)[match(itanes1996$q133, party_code_96)])),
    by.x = 'q133',
    by.y = 'Var1',
    all.x = TRUE,
    all.y = FALSE
  )
names(parties_immigration_96)[3:4] <- c("Freq","n")
  

## 2001 

# Position on immigration
# B1bis. Among the problems previously listed, you considered the following “very important”: [INTERVIEWER: read them]. Which one of them do you consider the most important? And which would be the next most important?
itanes2001$immigration_first <- with(itanes2001, b1bis.1 == 8)
itanes2001$immigration_second <- with(itanes2001, b1bis.2 == 8)
itanes2001$immigration_first_or_second <- 
  with(itanes2001, b1bis.1 == 8 | b1bis.2 == 8)

party_code_01 <-
  c(
    "Non risponde" = 1,
    "Alleanza Nazionale" = 2,
    "Ccd-Cdu (Biancofiore)" = 3,
    "Comunisti italiani" = 4,
    "Democratici di sinistra" = 5,
    "Democrazia europea - D Antoni" = 6,
    "Fiamma tricolore" = 7,
    "Forza Italia" = 8,
    "Il Girasole (Verdi - Sdi)" = 9,
    "La Margherita - Democrazia e  Liberta  con Rutelli" = 10,
    "Lega Nord" = 11,
    "Lista Bonino Radicali" = 12,
    "Lista Di Pietro - Italia dei valori" = 13,
    "Rifondazione comunista" = 14,
    "Altro (specificare)" = 15,
    "Altro non specificato" = 16,
    "Ha votato scheda bianca nulla" = 17,
    "Non sa o non ricorda" = 18
  )

centreright_parties_01 <- c(2, 7, 8, 11)

itanes2001$voted <- with(itanes2001, e20 > 1 & e20 < 17) 
itanes2001$voted_lega <- with(itanes2001, e20 == 11)
itanes2001$voted_lega[itanes2001$voted == FALSE] <- NA
itanes2001$voted_right <- with(itanes2001, e20 %in% centreright_parties_01)
itanes2001$voted_right[itanes2001$voted == FALSE] <- NA

with(itanes2001, prop.table(table(voted_lega, immigration_first), 1))
# immigration_first
# voted_lega      FALSE       TRUE
# FALSE 0.95213675 0.04786325
# TRUE  0.85185185 0.14814815
chisq.test(with(itanes2001, table(voted_lega, immigration_first)))
# p-value = 0.002329

with(itanes2001, prop.table(table(voted_lega, immigration_first_or_second), 1))
# immigration_first_or_second
# voted_lega     FALSE      TRUE
# FALSE 0.8875516 0.1124484
# TRUE  0.7222222 0.2777778
chisq.test(with(itanes2001, table(voted_lega, immigration_first_or_second)))
# p-value = 0.0003824

rq1_2001_tbl <-
  with(subset(itanes2001, voted_right == TRUE), 
       prop.table(table(voted_lega, immigration_first_or_second), 1))

rq1_2001_pvalue <- 
  chisq.test(with(subset(itanes2001, voted_right == TRUE),
                  table(voted_lega, immigration_first_or_second)))$p.value

parties_immigration_01 <-
  as.data.frame(with(subset(itanes2001, voted == TRUE), 
                     prop.table(table(e20, immigration_first_or_second), 1)))
parties_immigration_01$e20 <- 
  names(party_code_01[match(parties_immigration_01$e20, party_code_01)])
parties_immigration_01 <- subset(parties_immigration_01, immigration_first_or_second == TRUE)

parties_immigration_01 <-
  merge(
    parties_immigration_01,
    as.data.frame(table(names(party_code_01)[match(itanes2001$e20, party_code_01)])),
    by.x = 'e20',
    by.y = 'Var1',
    all.x = TRUE,
    all.y = FALSE
  )
names(parties_immigration_01)[3:4] <- c("Freq","n")


## 2006
## For consistency only questionario C
n_cases = 2011 # declared by itanes document
itanes2006 <- subset(itanes2006, !is.na(itanes2006$C133))
nrow(itanes2006) == n_cases # Check

# Voted lega at Camera
party_code_06 <- c(
  "Rifondazione comunista" = 1,
  "Verdi + Comunisti italiani" = 3,
  "Ulivo - Ds e Margherita" = 5,
  "Socialisti democratici e Radicali - Rosa nel pugno" = 6,
  "Lista Di Pietro" = 8,
  "Udeur di Mastella" = 9,
  "Udc di Casini" = 11,
  "Forza Italia" = 12,
  "Alleanza nazionale" = 13,
  "Nuova Dc + Psi di De Michelis" = 14,
  "Lega Nord" = 15,
  "Alternativa sociale con Alessandra Mussolini" = 16,
  "Repubblicani di La Malfa" = 17,
  "Partito pensionati (NB era nella coaliz di centrosin)" = 18,
  "Ha votato altra lista" = 96,
  "Non vuole rispondere" = 99
)

# Voting
itanes2006$voted <- with(itanes2006, C140 <= 96) # And declared vote
itanes2006$voted_lega <- with(itanes2006, C140 == 15)
itanes2006$voted_lega[itanes2006$voted == FALSE] <- NA
# with(itanes2006, sum(voted_lega,na.rm = T) / sum(voted,na.rm = T))

centreright_parties_06 <- c(12, 13, 15, 16)
itanes2006$voted_right <-  with(itanes2006, C140 %in% centreright_parties_06)
itanes2006$voted_right[itanes2006$voted == FALSE] <- NA

# I shall now read some opinions often people hold on politics and economics. Please tell me how much do 
# you agree with each of them. Tell me whether you disagree completely, agree a little, fairly agree or 
# agree completely. 
# Per niente d accordo	= 1
# Poco d accordo	= 2
# Abbastanza d accordo	= 3
# Molto d accordo	= 4
# Non sa	= 5
# Non risponde	= 6
# C10_2 Immigrants are a threat for our culture and our identity
itanes2006$C10_2_agree <- NA
itanes2006$C10_2_agree[itanes2006$C10_2 %in% c(3,4)] <- TRUE
itanes2006$C10_2_agree[itanes2006$C10_2 %in% c(1,2)] <- FALSE

itanes2006$C10_2_scalar <- as.numeric(itanes2006$C10_2)
itanes2006$C10_2_scalar[itanes2006$C10_2 > 4] <- NA

with(itanes2006, prop.table(table(voted_lega, C10_2_agree), 1))
# C10_2_agree
# voted_lega    FALSE     TRUE
# FALSE 0.477551 0.522449
# TRUE  0.212766 0.787234
chisq.test(with(itanes2006, table(voted_lega, C10_2_agree)))
# p-value = 0.0005818
with(itanes2006, t.test(C10_2_scalar[voted_lega == T], C10_2_scalar[voted_lega == F]))
# p-value = 2.266e-05
# mean of x mean of y 
# 3.191489  2.518367 

rq1_2006_t.test <-  
  with(subset(itanes2006, voted_right == TRUE), 
       t.test(C10_2_scalar[voted_lega == T], C10_2_scalar[voted_lega == F]))

require(dplyr)
itanes2006$C140_named <- names(party_code_06)[match(itanes2006$C140,
                                                    party_code_06)]
parties_immigration_06 <- 
  subset(itanes2006, voted == TRUE) %>%
  group_by(C140_named) %>%
  summarize(mean_C10_2_scalar = mean(C10_2_scalar,na.rm=T), 
            n = n())  

## 2008
party_code_08 <- 
  c("Sinistra Arcobaleno (Bertinotti)" = 1,
    "Partito Democratico (Veltroni)" = 2,
    "Italia dei Valori (Di Pietro)" = 3,
    "Popolo delle Libertà (Berlusconi)" = 4,
    "Lega (Bossi)" = 5,
    "Unione di Centro (Casini)" = 6,
    "La Destra (Santanchè-Storace)" = 7,
    "Partito Socialista (Boselli)" = 8,
    "Movimento Per l'Autonomia (Lombardo)" = 81,
    "Aborto? No, Grazie (Ferrara)" = 82,
    "Altro" = 87,
    "Non ricorda" = 88,
    "non risponde" = 99)

centreright_parties_08 <- c(4, 5, 7, 82)

itanes2008$voted <- itanes2008$D123 < 87
itanes2008$voted_lega <- itanes2008$D123 == 5
itanes2008$voted_lega[itanes2008$voted == FALSE] <- NA
sum(itanes2008$voted_lega, na.rm=T) / sum(itanes2008$voted, na.rm=T)
itanes2008$voted_right <- with(itanes2008, D123 %in% centreright_parties_08)
itanes2008$voted_right[itanes2008$voted == FALSE] <- NA

# Now, I am going to read some opinions on politics and the economy that people 
# sometimes express. Could you please tell me how much do 
# you agree (not at all, a little, farly much, very much) with each of them? 
# D007_10 Immigrants are a threat to our culture 
# 1	per niente d'accordo
# 2	poco d'accordo
# 3	abbastanza d'accordo
# 4	molto d'accordo
# 88	non sa
# 99	non risponde

itanes2008$D007_10_agree <- NA
itanes2008$D007_10_agree[itanes2008$D007_10 %in% c(3,4)] <- TRUE
itanes2008$D007_10_agree[itanes2008$D007_10 %in% c(1,2)] <- FALSE

itanes2008$D007_10_scalar <- as.numeric(itanes2008$D007_10)
itanes2008$D007_10_scalar[itanes2008$D007_10 > 4] <- NA

with(itanes2008, prop.table(table(voted_lega, D007_10_agree), 1))
# D007_10_agree
# voted_lega     FALSE      TRUE
# FALSE 0.6141898 0.3858102
# TRUE  0.4109589 0.5890411
chisq.test(with(itanes2008, table(voted_lega, D007_10_agree)))
# p-value = 1.722e-06
with(itanes2008, t.test(D007_10_scalar[voted_lega == T], D007_10_scalar[voted_lega == F]))
# p-value = 2.874e-07
# mean of x mean of y 
#  2.684932  2.204870 

rq1_2008_t.test <- 
  with(subset(itanes2008, voted_right == TRUE), 
       t.test(D007_10_scalar[voted_lega == T], D007_10_scalar[voted_lega == F]))

require(dplyr)
itanes2008$D123_named <- names(party_code_08)[match(itanes2008$D123,
                                                    party_code_08)]
parties_immigration_08 <- 
  subset(itanes2008, voted == TRUE) %>%
  group_by(D123_named) %>%
  summarize(mean_D007_10_scalar = mean(D007_10_scalar,na.rm=T), 
            n = n())

# 2013
party_code_13 <- c(
  "Rivoluzione Civile" = 1,
  "Sinistra Ecologia Liberta'" = 2,
  "Partito Democratico" = 3,
  "Lista Amnistia Giustizia Liberta'" = 4,
  "Movimento 5 Stelle Beppegrillo.It" = 5,
  "Svp" = 6,
  "Centro Democratico" = 7,
  "Scelta Civica Con Monti Per L'italia" = 8,
  "Unione Di Centro" = 9,
  "Fare Per Fermare Il Declino" = 10,
  "Futuro E Liberta'" = 11,
  "Il Popolo Della Liberta'" = 12,
  "Lega Nord" = 13,
  "Fiamma Tricolore" = 14,
  "La Destra" = 15,
  "Fratelli D'italia" = 16,
  "Grande Sud - Mpa" = 17,
  "Partito Pensionati" = 18,
  "Mir - Moderati In Rivoluzione" = 19,
  "Forza Nuova" = 20,
  "Partito Comunista Dei Lavoratori" = 21,
  "Casapound Italia" = 22,
  "Il Megafono Di Crocetta" = 23,
  "Scheda Bianca/Nulla/Non Ha Votato	" = 24,
  "altro" = 25,
  "Scheda Bianca/Nulla/Non Ha Votato" = 26,
  "Non Indica, Non Vuole Risponde" = 27)

centreright_parties_13 <- c(11, 12, 13, 14, 15, 16, 20, 22)

itanes2013$voted <- itanes2013$d90 < 24 & itanes2013$d90 > 0
itanes2013$voted_lega <- itanes2013$d90 == 13
itanes2013$voted_lega[itanes2013$voted == FALSE] <- NA

itanes2013$voted_right <- with(itanes2013, d90 %in% centreright_parties_13)
itanes2013$voted_right[itanes2013$voted == FALSE] <- NA

# D32. Alcuni dicono che riceviamo troppi immigrati. Altri dicono che va bene come 
# adesso. Altri ancora dicono che potremmo facilmente accoglierne di più. 
# Lei dove collocherebbe la sua opinione?
# d32	
# 1.00	Riceviamo troppi immigrati
# 7.00	Potremmo facilmente accoglierne di più
# 8.00	Non sa
# 9.00	Non risponde
itanes2013$d32_scalar <- as.numeric(itanes2013$d32)
itanes2013$d32_scalar[itanes2013$d32_scalar > 7] <- NA

with(itanes2013, t.test(d32_scalar[voted_lega == T], d32_scalar[voted_lega == F]))
# p-value = 0.01957
# mean of x mean of y 
# 1.857143  2.502098 

rq1_2013_t.test <- 
  with(subset(itanes2013, voted_right == TRUE), 
       t.test(d32_scalar[voted_lega == T], d32_scalar[voted_lega == F]))
  
require(dplyr)
itanes2013$d90_named <- names(party_code_13)[match(itanes2013$d90,
                                                    party_code_13)]
parties_immigration_13 <-
  subset(itanes2013, voted == TRUE) %>%
  group_by(d90_named) %>%
  summarize(mean_d32_scalar = mean(d32_scalar, na.rm=T),
            n = n())

# D16B. E quanta fiducia ha negli immigrati?
# d16b	
# 1.00	Nessuna fiducia
# 2.00	Poca fiducia
# 3.00	Abbastanza fiducia
# 4.00	Molta fiducia
# 5.00	Non sa
# 6.00	Non risponde
itanes2013$d16b_scalar <- as.numeric(itanes2013$d16b)
itanes2013$d16b_scalar[itanes2013$d16b_scalar > 4] <- NA

with(itanes2013, t.test(d16b_scalar[voted_lega == T], d16b_scalar[voted_lega == F]))
# p-value = 0.01957
# mean of x mean of y 
# 1.857143  2.502098 

with(subset(itanes2013, voted_right == TRUE), 
     t.test(d16b_scalar[voted_lega == T], d32_scalar[voted_lega == F]))


subset(itanes2013, voted == TRUE) %>%
  group_by(d90_named) %>%
  summarize(mean_d16b_scalar = mean(d16b_scalar, na.rm=T),
            n = n())

